package com.wanlong.udp;

import android.net.Uri;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import butterknife.BindView;

/**
 * @author lingchen
 * @date 2019/3/29
 * mail:lingchen52@foxmail.com
 */
public class PlayActivity extends BaseActivity {
    @BindView(R.id.videoview)
    FullscreenVideoView mVideoview;

    @Override
    protected int getContentResId() {
        return R.layout.activity_play;
    }

    @Override
    protected void initView() {

    }

    private String url;

    @Override
    protected void initData() {
        url = getIntent().getStringExtra("url");
        initVideoView();
    }

    private void initVideoView() {
        mVideoview.setVideoURI(Uri.parse(url));
        mVideoview.setOnPreparedListener(mp -> mp.start());
        mVideoview.setOnCompletionListener(mp -> {

        });
        mVideoview.setOnErrorListener((mp, what, extra) -> {
            Log.d("PlayActivity", "");
            Toast.makeText(PlayActivity.this, "节目无法播放", Toast.LENGTH_LONG).show();
            return true;
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        mVideoview.stopPlayback();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
